const pool = require('../modules/tools/getPool');

const createAdmin = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('CREATE TABLE `news`.`admin` ( `id` INT NOT NULL AUTO_INCREMENT, `account` VARCHAR(20) NOT NULL, `password` VARCHAR(20) NOT NULL, `able` TINYINT NOT NULL DEFAULT 1, `name` VARCHAR(20) NULL, PRIMARY KEY (`id`), UNIQUE `account_UNIQUE` (`account` ASC) );', (error, rows, fields)=>{
			if (error) {
				reject(error,1)
			}
			resolve(rows)
		})
    })
}

const createUser = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('CREATE TABLE `news`.`user` ( `id` INT NOT NULL AUTO_INCREMENT, `account` VARCHAR(20) NOT NULL, `password` VARCHAR(20) NOT NULL, `headimg` VARCHAR(45) NULL, `name` VARCHAR(20) NULL, `able` TINYINT NOT NULL DEFAULT 1, `createtime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE `account_UNIQUE` (`account` ASC) );', (error, rows, fields)=>{
			if (error) {
				reject(error,1)
			}
			resolve(rows)
		})
    })
}

const createModule = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('CREATE TABLE `news`.`module` ( `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(20) NOT NULL, `able` TINYINT NOT NULL DEFAULT 1, `createtime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE `name_UNIQUE` (`name` ASC) );', (error, rows, fields)=>{
			if (error) {
				reject(error,1)
			}
			resolve(rows)
		})
    })
}

const createNews = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('CREATE TABLE `news`.`news`( `id` INT NOT NULL AUTO_INCREMENT, `able` TINYINT NOT NULL DEFAULT 1, `createtime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `author_id` INT NOT NULL, `module_id` INT NOT NULL, `coverimg` VARCHAR(45) NOT NULL, `title` VARCHAR(45) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `fk_author_id` FOREIGN KEY (`author_id`) REFERENCES `news`.`admin` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_module_id` FOREIGN KEY (`module_id`) REFERENCES `news`.`module` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION);', (error, rows, fields)=>{
			if (error) {
				reject(error,1)
			}
			resolve(rows)
		})
    })
}

const createComment = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('CREATE TABLE `news`.`comment`( `id` INT NOT NULL AUTO_INCREMENT, `able` TINYINT NOT NULL DEFAULT 1, `createtime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `author_id` INT NOT NULL, `news_id` INT NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `fk2_author_id` FOREIGN KEY (`author_id`) REFERENCES `news`.`user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_news_id` FOREIGN KEY (`news_id`) REFERENCES `news`.`news` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION);', (error, rows, fields)=>{
			if (error) {
				reject(error,1)
			}
			resolve(rows)
		})
    })
}



const insertAdmin = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('INSERT INTO admin (name,account,password) VALUES ("张三","admin","123123123");', (error, rows, fields)=>{
			if (error) {
				reject(error,1)
			}
			resolve(rows)
		})
    })
}



pool.getConnection((err, connection)=>{
	if (err) {
		console.error(err);
		return;
	}

	const start = async function(){

		await createAdmin(connection);
		await createUser(connection);
		await createModule(connection);
		await createNews(connection);
		await createComment(connection);
		await insertAdmin(connection);

	}

	start().then(v=>{

		console.log("全部执行成功");

	})
	.catch((e,n)=>{
		console.log(e);
		console.log("数据库执行错误，在第"+n+"条");
	})

});